<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计数页面与表格</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f5f5f5;
        }

        .counter {
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 36px;
            margin-bottom: 20px;
        }

        table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 20px;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="counter">
        发送邮件数
        <h1>{{count}}</h1>
        <!--        <button  id="myButton">发送邮件</button>-->
        <table>
            <tr>
                <th>发送者</th>
                <th>接收者</th>
                <th>标题</th>
                <th>发送时间</th>
            </tr>
            <tr v-for="item in tableData" :key="item.id">
                <td>{{item.sender}}</td>
                <td>{{item.receiver}}</td>
                <td>{{item.title}}</td>
                <td>{{item.sendTime}}</td>
            </tr>
        </table>
    </div>
    <div class="counter">
        请求验证码数量
        <h1>{{telcount}}</h1>
        <table>
            <tr>
                <th>号码</th>
                <th>发送时间</th>
            </tr>
            <tr v-for="item in tableTelData" :key="item.id">
                <td>{{item.senderTel}}</td>
                <td>{{item.sendTime}}</td>
            </tr>
        </table>
    </div>
</div>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/axios.js"></script>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            count: 0,
            tableData: [
                {sender: '', receiver: '', title: '', sendTime: ''}
            ],
            tableTelData: [
                {senderTel: '',  sendTime: ''}
                ],
            socket: null,
            telcount: 0
        },
        created() {
            // 创建 WebSocket 连接，假设后端服务运行在本地端口 8080
            this.socket = new WebSocket('ws://localhost:8088/websocket');
            this.socket.onmessage = (event) => {
                console.log(event);
                const data = JSON.parse(event.data);
                // 根据后端消息更新计数和表格数据
                let temp = {
                    sender: data.boss,
                    receiver: data.consume,
                    title: data.dishList,
                    sendTime: data.sendTime
                }
                let tempTel = {
                    senderTel: data.tel,
                    sendTime: data.sendTelTime
                }
                this.updateTableData(temp)
                this.updateTelTableData(tempTel)
            };
        },
        beforeDestroy() {
            // 在组件销毁前关闭 WebSocket 连接
            if (this.socket) {
                this.socket.close();
            }
        },
        methods: {
            incrementCount() {
                this.count++;
            },
            updateTableData( temp ) {
                if ( temp.sender!=null ){
                    this.tableData.push(temp)
                    this.count++;
                }

            },
            updateTelTableData( tempTel ) {
                if ( tempTel.senderTel!=null ){
                    this.tableTelData.push(tempTel)
                    this.telcount++;
                }
            }

        },

    });


</script>
</body>

</html>